/*
* ExcelReplaceData.java
* Created on  2014-5-13 上午9:13
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-5-13       gaoxinyu    初始版本
*
*/
package com.infitecs.eyas.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author gaoxinyu
 * @version 1.0.1
 */
public class ExcelReplaceData {
    private int row;// Excel单元格行
    private int column;// Excel单元格列
    private Object value;// 替换的文本或者时间

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    /**
     * 替换Excel模板文件内容
     *
     * @param dataList 文档数据
     */
    public static boolean replaceModel(List<ExcelReplaceData> dataList, HttpServletRequest request, HttpServletResponse response, ExcelInfo excelInfo) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("/") + "model" + File.separator + excelInfo.getImportName();
        OutputStream os = response.getOutputStream();// 取得输出流
        response.reset();// 清空输出流
        response.setHeader("Content-Disposition", "attachment; filename=" + new String((excelInfo.getOutName()+DateUtil.getSystemDate(DateUtil.yyyyMMddHHmmssSSS)+".xlsx").getBytes(), "iso-8859-1"));
        response.setContentType("application/msexcel");// 定义输出类型
        boolean bool = true;
        try {
            FileInputStream is = new FileInputStream(path);
            Workbook wb = new XSSFWorkbook(is);
            Sheet sheet = wb.getSheetAt(0);
            for (ExcelReplaceData data : dataList) {
                //获取单元格内容
                Row row = sheet.getRow(data.getRow());
                Cell cell = row.getCell((short) data.getColumn());
                //写入单元格内容
                Object value = data.getValue();
                if (value instanceof Date) {
                    cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
                    //                  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue((Date) value);
                } else {
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    //                  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue((String) value);
                }
            }

            // 输出文件
            wb.write(os);
            is.close();
            os.flush();
            os.close();
        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        }
        return bool;
    }


}
